package ten;

public class ThreadLocalTest {
    //    private static ThreadLocal<Integer> threadLocal = new ThreadLocal<>();
//    private static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>() {
//        @Override
//        protected Integer initialValue() {
//            return 200;
//        }
//    };

    private static ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(() -> 200);

    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName() + "--->" + threadLocal.get());
        threadLocal.set(99);
        System.out.println(Thread.currentThread().getName() + "--->" + threadLocal.get());
        new Thread(new MyRun(), "MyRun1").start();
        new Thread(new MyRun(), "MyRun2").start();
    }

    public static class MyRun implements Runnable{
        @Override
        public void run() {
            threadLocal.set((int) (Math.random()*99));
            System.out.println(Thread.currentThread().getName() + "--->" + threadLocal.get());
        }
    }
}
